package com.jingxi.rest.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingxi.common.util.JsonUtils;
import com.jingxi.mapper.TbContentMapper;
import com.jingxi.mapper.TbItemMapper;
import com.jingxi.model.TbContent;
import com.jingxi.model.TbContentExample;
import com.jingxi.model.TbContentExample.Criteria;
import com.jingxi.model.TbItem;
import com.jingxi.model.TbItemExample;
import com.jingxi.rest.dao.JedisClient;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private TbItemMapper tbItemMapper;

	@Autowired
	private JedisClient jedisClient;

	private static final String INDEX_CONTENT_REDIS_KEY = "INDEX_CONTENT_REDIS_KEY";

	@Override
	public List<TbContent> getContentList(long contentCid) {
		// 从缓存中取内容
		try {
			String result = jedisClient.hget(INDEX_CONTENT_REDIS_KEY, contentCid + "");
			if (!StringUtils.isBlank(result)) {
				// 把字符串装换成list
				List<TbContent> resultList = JsonUtils.jsonToList(result, TbContent.class);
				return resultList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 更具内容分类id查询内容列表
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(contentCid);
		// 执行查询
		List<TbContent> list = contentMapper.selectByExample(example);
		// 向缓存中添加内容
		try {
			//把list转换成字符串
			String cacheString = JsonUtils.objectToJson(list);
			jedisClient.hset(INDEX_CONTENT_REDIS_KEY, contentCid + "", cacheString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<TbItem> showNewList() {
		TbItemExample example = new TbItemExample();
		com.jingxi.model.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andIsNewEqualTo(1);
		List<TbItem> list = tbItemMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public List<TbItem> showHotList() {
		TbItemExample example = new TbItemExample();
		com.jingxi.model.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andIsHotEqualTo(1);
		List<TbItem> list = tbItemMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<TbItem> showClothesList() {
		TbItemExample example = new TbItemExample();
		com.jingxi.model.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andCidBetween(2l, 3l);
		List<TbItem> list = tbItemMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<TbItem> showGiftList() {
		TbItemExample example = new TbItemExample();
		com.jingxi.model.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andCidEqualTo(4l);
		List<TbItem> list = tbItemMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<TbItem> showFlowerList() {
		TbItemExample example = new TbItemExample();
	    com.jingxi.model.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andCidEqualTo(5l);
		List<TbItem> list = tbItemMapper.selectByExample(example);
		return list;
	}

}
